﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.ComponentModel;
using System;
using System.IO;
using System.Reflection;

namespace MyMesComp
{
    internal static class UserControlExtension
    {
        public static T As<T>(this object obj)
        {
            if (obj == null)
            {
                return default(T);
            }
            return (T)obj;
        }
        public static string ToJson(this object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }
        public static T Deserialize<T>(this string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        public static bool IsJObject(this JToken tolen)
        {
            return tolen is JObject;
        }
        public static bool IsJArray(this JToken tolen)
        {
            return tolen is JArray;
        }

        public static string ToJsonFormatted(this object obj1)
        {
            var settings = new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore,
                Formatting = Formatting.Indented,
            };
            string json = JsonConvert.SerializeObject(obj1, settings);

            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(json);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            string formatString;
            if (obj != null)
            {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                {
                    Formatting = Formatting.Indented,
                    Indentation = 4,
                    IndentChar = ' ',
                };
                serializer.Serialize(jsonWriter, obj);
                formatString = textWriter.ToString();
            }
            else
            {
                formatString = json;
            }
            return formatString;
        }

        public static string GetEnumDescription(this Enum en)
        {
            if (en == null) return null;
            Type type = en.GetType();   //获取类型
            FlagsAttribute customAttribute = type.GetCustomAttribute<FlagsAttribute>();
            if (customAttribute == null)
            {
                MemberInfo[] memberInfos = type.GetMember(en.ToString());   //获取成员
                if (memberInfos.Length > 0)
                {
                    if (memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) is DescriptionAttribute[] attrs)
                    {
                        if (attrs.Length > 0)
                        {
                            return attrs[0].Description; //返回当前描述
                        }
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
                return en.ToString();
            }
            else//有Flags
            {

            }
            return null;
        }
    }
}